﻿using HslCommunication;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CentralVacuum.Common
{
    public class LanguageRESX
    {
        /// <summary>
        /// set the toolscript
        /// </summary>
        /// <param name="item"></param>
        /// <param name="resources"></param>
        private static void AppLang(ToolStripMenuItem item, ComponentResourceManager resources)
        {
            if (item is ToolStripMenuItem)
            {
                resources.ApplyResources(item, item.Name);
                ToolStripMenuItem tsmi = item;
                if (tsmi.DropDownItems.Count > 0)
                {
                    foreach (ToolStripMenuItem c in tsmi.DropDownItems)
                    {
                        AppLang(c, resources);
                    }
                }
            }
        }

        /// <summary>
        /// 访问窗体所有的控件
        /// </summary>
        /// <param name="fatherControl">控件</param>
        /// <param name="resources">资源</param>
        public static void AppLang(Control fatherControl, ComponentResourceManager resources)
        {

            if (fatherControl is UIContextMenuStrip) 
            {
                resources.ApplyResources(fatherControl, fatherControl.Name);
                UIContextMenuStrip ms = (UIContextMenuStrip)fatherControl;

                if (ms.Items.Count > 0)
                {
                    foreach (ToolStripMenuItem c in ms.Items)
                    {
                        AppLang(c, resources);
                    }
                }
            }

            foreach (Control c in fatherControl.Controls)//普通控件对语言处理
            {
                resources.ApplyResources(c, c.Name);
                AppLang(c, resources);
            }
        }

        /// <summary>
        /// 切换中文
        /// </summary>
        /// <param name="form">窗口</param>
        public static void Resource_zh_CN()
        {
            UILocalizeHelper.SetCH();//配置SunnyUI的系统翻译
            StringResources.SetLanguageChinese();//配置HslCommunication的系统翻译
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");//

            //ComponentResourceManager resources = new ComponentResourceManager(typeof(FCustomGlobal));
            //ApplyResource(form, resources);
            Properties.Settings.Default.Language = 0;//中文
            Properties.Settings.Default.Save();//使用Save方法保存更改
        }

        /// <summary>
        /// 切换英文
        /// </summary>
        /// <param name="form">窗口</param>
        public static void Resource_en_US()
        {
            UILocalizeHelper.SetEN();//配置SunnyUI的系统翻译
            StringResources.SeteLanguageEnglish();//配置HslCommunication的系统翻译
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            
            //ComponentResourceManager resources = new ComponentResourceManager(typeof(FCustomGlobal));
            //ApplyResource(form, resources);
            Properties.Settings.Default.Language = 1;//英文
            Properties.Settings.Default.Save();//使用Save方法保存更改
        }

        /// <summary>
        /// 动态切换中文
        /// </summary>
        /// <param name="form">窗口</param>
        public static void dynamic_Resource_zh_CN(Form form)
        {
            UILocalizeHelper.SetCH();//配置SunnyUI的系统翻译
            StringResources.SetLanguageChinese();//配置HslCommunication的系统翻译
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");//
            if (form != null)
            {
                ComponentResourceManager resources = new ComponentResourceManager(typeof(FCustomGlobal));
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
            Properties.Settings.Default.Language = 0;//中文
            Properties.Settings.Default.Save();//使用Save方法保存更改
        }

        /// <summary>
        /// 动态切换英文
        /// </summary>
        /// <param name="form">窗口</param>
        public static void dynamic_Resource_en_US(Form form)
        {
            UILocalizeHelper.SetEN();//配置SunnyUI的系统翻译
            StringResources.SeteLanguageEnglish();//配置HslCommunication的系统翻译
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            if (form != null)
            {
                ComponentResourceManager resources = new ComponentResourceManager(typeof(FCustomGlobal));
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
            Properties.Settings.Default.Language = 1;//英文
            Properties.Settings.Default.Save();//使用Save方法保存更改
        }
    }
}
